package com.example.immersivedemo

import android.graphics.Color
import android.media.tv.TvContract.Programs
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale.Companion.FillBounds
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.immersivedemo.ui.theme.ComposeNewStudyTheme

class MainActivity : ComponentActivity() {

    // 创建底部导航栏的项目
    val items = listOf(
        "首页",
        "配置",
        "我的"
    )


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            var isDark by remember {
                mutableStateOf(false)
            }

            var selectedIndex by remember {
                mutableStateOf(0)
            }
            // TODO: isDark主要是修改状态栏文字颜色
            DisposableEffect(isDark) {
                enableEdgeToEdge(
                    SystemBarStyle.auto(Color.TRANSPARENT, Color.TRANSPARENT) { isDark },
                    SystemBarStyle.auto(Color.WHITE, Color.BLACK) { isDark },
                )
                onDispose { }
            }

            ComposeNewStudyTheme {

                Scaffold(
                    modifier = Modifier.fillMaxSize(),
                    contentWindowInsets = WindowInsets(0, 0, 0, 0),//页面沉浸式
                    bottomBar = {
                        NavigationBar {
                            items.forEachIndexed { index, s ->
                                NavigationBarItem(
                                    label = { Text(text = s) },
                                    selected = selectedIndex == index,
                                    onClick = {
                                        selectedIndex = index
                                        isDark = index == 2
                                    },
                                    icon = { })
                            }
                        }
                    }
                ) { _ ->
                    Box(
                        modifier = Modifier
                            .fillMaxSize()
                    ) {
                        when (selectedIndex) {
                            0 -> {
                                Home()
                            }

                            1 -> {
                                Profile()
                            }

                            2 -> {
                                Me()
                            }
                        }
                    }
                }
            }
        }
    }
}


@Composable
fun Home(modifier: Modifier = Modifier) {
    Text(text = "这是首页")
}

@Composable
fun Profile(modifier: Modifier = Modifier) {
    Text(text = "这是配置")
}

@Composable
fun Me(modifier: Modifier = Modifier) {
    Image(
        painter = painterResource(id = R.drawable.b3080f9782ce6247),
        modifier = Modifier
            .fillMaxWidth()
            .height(200.dp),
        contentScale = FillBounds,
        contentDescription = null
    )
}